package com.hmyd.hmyd_android.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.adapter.RecommendListAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.Page;
import com.hmyd.hmyd_android.dto.Recommend;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.listview.XListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class RecommendListFragment extends BasicFragmentList {

	private XListView mList;
	private TextView recommendexplain;
	private RecommendListAdapter mRecommendListAdapter;

	private final int REFRESH = 1;
	private final int LOADMORE = 2;

	protected boolean hasMore = false;
	private String cursorId;

	private List<Recommend> listData = new ArrayList<Recommend>();

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View investmentLayout = inflater
				.inflate(R.layout.fragment_recommend_list, container, false);

		initViews(investmentLayout);
		initListener();

		return investmentLayout;
	}

	public XListView getXList() {
		return mList;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (!isLoading && listData.size() == 0) {
			onRequest(REFRESH, UrlConstant.RECOMMENDLIST, HttpMethod.GET, null);
		}
	}

	private void initViews(View rootLayout) {
		mList = (XListView) rootLayout.findViewById(R.id.list);
		recommendexplain = (TextView) rootLayout.findViewById(R.id.recommendexplain);
		mList.setPullRefreshEnable(true); // 允许下拉刷新
		mList.setPullLoadEnable(false); // 禁止加载更多
		mList.setAutoLoadEnable(false); // 禁止自动加载
		mList.setXListViewListener(this);// 加载监听

		mRecommendListAdapter = new RecommendListAdapter(getActivity(), listData);
		mList.setAdapter(mRecommendListAdapter);
	}

	@Override
	public void onRefresh() {
		super.onRefresh();
		onRequest(REFRESH, UrlConstant.RECOMMENDLIST, HttpMethod.GET, null);
	}

	@Override
	public void onLoadMore() {
		super.onLoadMore();

		RequestParams params = new RequestParams();
		params.addBodyParameter("lastCursor", cursorId);

		onRequest(LOADMORE, UrlConstant.RECOMMENDLIST, HttpMethod.GET, params);
	}

	public void onPostSuccess(int postId, String result) {
		super.onPostSuccess(postId, result);

		Gson gson = new Gson();
		java.lang.reflect.Type type2 = new TypeToken<Page<Recommend>>() {
		}.getType();
		Page<Recommend> page = gson.fromJson(result, type2);
		hasMore = page.hasMore;

		if (page != null && page.records != null) {
			switch (postId) {
			case REFRESH:
				listData.clear();
				listData.addAll(page.records);
				break;
			case LOADMORE:
				listData.addAll(page.records);
				break;
			default:
				break;
			}

			if (listData.size() > 0) {
				cursorId = listData.get(listData.size() - 1).cursorId;
			} else {
				cursorId = null;
			}

			mRecommendListAdapter.notifyDataSetChanged();
		}

		if (getXList() != null && hasMore) {
			getXList().setPullLoadEnable(true);
		} else if (getXList() != null && !hasMore) {
			getXList().setPullLoadEnable(false);
		}
	}

	public void onPostFailure(int postId, String msg) {
		super.onPostFailure(postId, msg);

		if (getXList() != null && hasMore) {
			getXList().setPullLoadEnable(true);
		} else if (getXList() != null && !hasMore) {
			getXList().setPullLoadEnable(false);
		}
	}

	private void initListener() {
		mList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// startActivity(new
				// Intent(getActivity(),InvestmentDetailActivity.class));
			}
		});
		recommendexplain.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (AppBasic.app != null && !TextUtils.isEmpty(AppBasic.app.referrerInstructionsLinkUrl)) {
					Utils.jumpURL(getActivity(), AppBasic.app.referrerInstructionsLinkUrl, "推荐说明", "", false);
				}
			}
		});
	}

}
